import { _decorator, Node, Component, Button, Label } from 'cc';
const { ccclass, property } = _decorator;

import { HeChengDaXiGua_GameStorage } from "./HeChengDaXiGua_GameStorage";

@ccclass('HeChengDaXiGuaSaveTest')
export default class HeChengDaXiGua_SaveTest extends Component {

    @property(Button)
    testSaveButton: Button | null = null;

    @property(Button)
    testLoadButton: Button | null = null;

    @property(Button)
    testClearButton: Button | null = null;

    @property(Button)
    testAutoSaveButton: Button | null = null; // 测试自动保存按钮

    @property(Label)
    infoLabel: Label | null = null;
    private autoSaveInterval: number = null; // 自动保存定时器
    onLoad() {
        this.addListeners();
    }
    addListeners() {
        if (this.testSaveButton) {
            this.testSaveButton.node.on(Node.EventType.TOUCH_END, this.testSave, this);
        }
        if (this.testLoadButton) {
            this.testLoadButton.node.on(Node.EventType.TOUCH_END, this.testLoad, this);
        }
        if (this.testClearButton) {
            this.testClearButton.node.on(Node.EventType.TOUCH_END, this.testClear, this);
        }
        if (this.testAutoSaveButton) {
            this.testAutoSaveButton.node.on(Node.EventType.TOUCH_END, this.testAutoSave, this);
        }
    }
    testSave() {
        // 模拟保存一些测试数据
        const testFruits = [
            { level: 1, x: 100, y: 200, scaleX: 1, scaleY: 1, rotation: 0, linearVelocityX: 0, linearVelocityY: 0, angularVelocity: 0 },
            { level: 2, x: 200, y: 300, scaleX: 1, scaleY: 1, rotation: 45, linearVelocityX: 10, linearVelocityY: -5, angularVelocity: 2 }
        ];

        HeChengDaXiGua_GameStorage.data.fruits = testFruits;
        HeChengDaXiGua_GameStorage.data.current_score = 150;
        HeChengDaXiGua_GameStorage.saveData();

        this.updateInfoLabel(`手动保存了 ${testFruits.length} 个水果，分数: 150`);
    }
    testLoad() {
        const data = HeChengDaXiGua_GameStorage.loadFruitsData();
        this.updateInfoLabel(`加载了 ${data.fruits.length} 个水果，分数: ${data.currentScore}`);
    }
    testClear() {
        HeChengDaXiGua_GameStorage.clearFruitsData();
        this.updateInfoLabel("已清除所有保存数据");
    }
    testAutoSave() {
        if (this.autoSaveInterval) {
            // 停止自动保存
            clearInterval(this.autoSaveInterval);
            this.autoSaveInterval = null;
            this.updateInfoLabel("已停止自动保存测试");
            if (this.testAutoSaveButton) {
                this.testAutoSaveButton.node.getComponentInChildren(Label).string = "开始自动保存测试";
            }
        } else {
            // 开始自动保存测试
            let saveCount = 0;
            this.autoSaveInterval = setInterval(() => {
                saveCount++;
                const testFruits = [
                    { level: 1, x: 100 + saveCount * 10, y: 200 + saveCount * 5, scaleX: 1, scaleY: 1, rotation: saveCount * 10, linearVelocityX: saveCount, linearVelocityY: -saveCount, angularVelocity: saveCount }
                ];

                HeChengDaXiGua_GameStorage.data.fruits = testFruits;
                HeChengDaXiGua_GameStorage.data.current_score = 100 + saveCount * 10;
                HeChengDaXiGua_GameStorage.saveData();

                this.updateInfoLabel(`自动保存 #${saveCount}: ${testFruits.length} 个水果，分数: ${100 + saveCount * 10}`);
            }, 2000); // 每2秒保存一次

            if (this.testAutoSaveButton) {
                this.testAutoSaveButton.node.getComponentInChildren(Label).string = "停止自动保存测试";
            }
        }
    }
    updateInfoLabel(text: string) {
        if (this.infoLabel) {
            this.infoLabel.string = text;
        }
        console.log(text);
    }
    onDestroy() {
        this.removeListeners();
        if (this.autoSaveInterval) {
            clearInterval(this.autoSaveInterval);
        }
    }
    removeListeners() {
        if (this.testSaveButton) {
            this.testSaveButton.node.off(Node.EventType.TOUCH_END, this.testSave, this);
        }
        if (this.testLoadButton) {
            this.testLoadButton.node.off(Node.EventType.TOUCH_END, this.testLoad, this);
        }
        if (this.testClearButton) {
            this.testClearButton.node.off(Node.EventType.TOUCH_END, this.testClear, this);
        }
        if (this.testAutoSaveButton) {
            this.testAutoSaveButton.node.off(Node.EventType.TOUCH_END, this.testAutoSave, this);
        }
    }
}